﻿Imports System.IO
Imports LuaInterface
Imports Microsoft.VisualBasic

Public Class clsAucScanAdapter

    Public Sub New()
        MyBase.New()
    End Sub

    Public Function Read_AucScanFromFilePath(ByVal AucScanFilePath As String) As System.Collections.Generic.List(Of AuctionItem)
        Dim LuaTableSearchPath As String = String.Empty
        Dim LuaTableServers As LuaTable = Nothing
        Dim LuaTableFactions As LuaTable = Nothing
        Dim LuaTableRopes As LuaTable = Nothing
        Dim LuaTableRopeItems As LuaTable = Nothing
        Dim LuaTableRopeItemsItemsIndividual As LuaTable = Nothing
        Dim lua As Lua = Nothing
        Dim sServerName As String = String.Empty
        Dim sFactionName As String = String.Empty
        Dim oEnumErator As System.Collections.IEnumerator = Nothing
        Dim oBJ() As Object = Nothing
        Dim oScanAuctionItems As System.Collections.Generic.List(Of AuctionItem) = New System.Collections.Generic.List(Of AuctionItem)
        Dim oAuctionItem As AuctionItem = Nothing
        Dim FileContents As String = String.Empty
        Dim CleanedFileContents As String = String.Empty
        Try
            Using sr As StreamReader = New StreamReader(AucScanFilePath)
                FileContents = sr.ReadToEnd
                sr.Close()
            End Using
            'replace all nil as it is causing a problem replace with a -1
            CleanedFileContents = FileContents.Replace("nil", "-1")
            'lua Engine constructor
            lua = New Lua()
            'read the file into a working object
            lua.DoString(CleanedFileContents)
            'get a collection of servers in the parsed file
            LuaTableServers = TryCast(lua.DoString("return AucScanData.scans")(0), LuaTable)
            If LuaTableServers Is Nothing Then
                Throw New ApplicationException("Invalid Auctioneer Scan Data File.")
            End If

            For Each DictionaryEntryServer As DictionaryEntry In LuaTableServers
                sServerName = TryCast(DictionaryEntryServer.Key, String)
                LuaTableFactions = TryCast(DictionaryEntryServer.Value, LuaTable)
                For Each DictionaryEntryFaction As DictionaryEntry In LuaTableFactions
                    sFactionName = TryCast(DictionaryEntryFaction.Key, String)
                    'get the rope for each server / each faction
                    LuaTableSearchPath = "AucScanData.scans." & sServerName & "." & sFactionName & ".ropes"
                    LuaTableRopes = lua.GetTable(LuaTableSearchPath)
                    If LuaTableRopes Is Nothing Then
                        Exit For
                    Else
                        'create the sub tables here
                        For Each DictionaryEntryRope As DictionaryEntry In LuaTableRopes
                            oBJ = lua.DoString(DictionaryEntryRope.Value)
                            LuaTableRopeItems = TryCast(oBJ(0), LuaInterface.LuaTable)
                            If Not IsNothing(LuaTableRopeItems) Then
                                For Each tItem As DictionaryEntry In LuaTableRopeItems
                                    LuaTableRopeItemsItemsIndividual = TryCast(tItem.Value, LuaInterface.LuaTable)
                                    oAuctionItem = AuctionItemReader.GET_AuctionItem(LuaTableRopeItemsItemsIndividual, sServerName, sFactionName)
                                    If Not IsNothing(oAuctionItem) Then
                                        oScanAuctionItems.Add(oAuctionItem)
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            Next
            LuaTableServers = Nothing
            LuaTableFactions = Nothing
            LuaTableRopes = Nothing
            LuaTableRopeItems = Nothing
            LuaTableRopeItemsItemsIndividual = Nothing
            lua.Dispose()
            lua = Nothing
            Return oScanAuctionItems
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

End Class
